import { AutoFormParams } from '@/types/utils/form';

export enum EnumCardColorCode {
  Color010 = '#63b359',
  Color020 = '#2c9f67',
  Color030 = '#509fc9',
  Color040 = '#5885cf',
  Color050 = '#9062c0',
  Color060 = '#d09a45',
  Color070 = '#e4b138',
  Color080 = '#ee903c',
  Color081 = '#f08500',
  Color082 = '#a9d92d',
  Color090 = '#dd6549',
  Color100 = '#cc463d',
  Color101 = '#cf3e36',
  Color102 = '#5E6671',
}

export const CardColors = [
  { code: 'Color010', color: EnumCardColorCode['Color010'] },
  { code: 'Color020', color: EnumCardColorCode['Color020'] },
  { code: 'Color030', color: EnumCardColorCode['Color030'] },
  { code: 'Color040', color: EnumCardColorCode['Color040'] },
  { code: 'Color050', color: EnumCardColorCode['Color050'] },
  { code: 'Color060', color: EnumCardColorCode['Color060'] },
  { code: 'Color070', color: EnumCardColorCode['Color070'] },
  { code: 'Color080', color: EnumCardColorCode['Color080'] },
  { code: 'Color081', color: EnumCardColorCode['Color081'] },
  { code: 'Color082', color: EnumCardColorCode['Color082'] },
  { code: 'Color090', color: EnumCardColorCode['Color090'] },
  { code: 'Color100', color: EnumCardColorCode['Color100'] },
  { code: 'Color101', color: EnumCardColorCode['Color101'] },
  { code: 'Color102', color: EnumCardColorCode['Color102'] },
];

export interface Card {
  card_id?: number;
  shop_id?: number;
  name?: string;
  /**
   * 卡的类型;
   * 1 无门槛卡(默认) 2 规则卡 3 购买卡
   */
  type?: 1 | 2 | 3;
  /**
   * 卡片样式
   * 1 背景色 2 背景图片
   */
  color_type?: 1 | 2;
  /**
   * 卡背景颜色
   */
  color_code?: string;
  /**
   * 背景图片
   */
  background_pic_url?: string;
  /** 使用须知 */
  description?: string;
  /** 客服电话 */
  service_phone?: string;
  /** 价格 */
  price?: number;
  quantity?: number;
  outer_id?: string;
  /** 自动发放规则 */
  rule_condition?: RuleCondition;
  /** 激活方式，0:不需要激活；1:仅需手机激活；2:手机+填写资料 */
  activate_mode?: 0 | 1 | 2;
  /** 生效开始时间，如："2017-03-07 00:00:00" */
  term_start_time?: string;
  /** 生效结束时间，如："2017-03-07 00:00:00" */
  term_end_time?: string;
  term_days?: number;
  /** 生效方式类型，1:从领取开始无期限；2:从固定时刻开始，到固定时刻结束； 3:从领取开始，持续一段时长(termDays) */
  term_type?: 1 | 2 | 3;
  /** 失效后，默认转变的会员卡，默认不转变 */
  term_to_card_alias?: string;
  /** 是否允许微信端分享 ，默认允许 */
  is_allow_share?: number;
  /** 微信卡包的同步设置 0:不同步；1:基于卡号核销；2:基于卡号和条形码；3:基于卡号和二维码 */
  sync_weixin_mode?: 0 | 1 | 2 | 3;
  /** 同步微信卡状态，1：不同步；2：审核中；3：通过；4：失败 */
  sync_weixin_status?: 1 | 2 | 3 | 4;
  /** 会员卡状态，1：正常；2：已禁用；3：已删除 */
  status?: 1 | 2 | 3;
  level?: number;
  /** 会员卡权益集合 */
  rights?: CardRight[];
  create_time?: number;
  update_time?: number;
}

export interface RuleCondition {
  /** 累积消费金额(金额单位为元) */
  amount_limit?: number;
  /** 累积积分 */
  points_limit?: number;
  /** 累积下单成功次数 */
  trade_limit?: number;
}

export const cardRightData: CardRight[] = [
  {
    type: 1,
    name: '包邮',
    is_enable: 0,
  },
  {
    type: 2,
    name: '消费折扣',
    is_enable: 0,
    ext_info: {
      discount: ''
    }
  },
];

/**
 * 权益类型
 * type = 1 包邮
 * type = 2 折扣
 * type = 3 送优惠券
 * type = 4 赠送积分
 */
export type CardRightType = 1 | 2 | 3 | 4;

export interface CardRight extends RightTemplate {
  right_id?: number;
  card_id?: number;
  type?: CardRightType;
  /**
   * 权益名称
   */
  name?: string;
  /**
   * 折扣权益字段，折数 ，1表示0.01折 范围选择：1-100, type为2时必传
   */
  discount?: number;
  /**
   * 赠送积分
   * 积分数量,type为4时必传
   */
  points?: number;
  ext_info?: object;
  /**
   * 扩展字段
   */
  is_enable: 0 | 1;
}

interface IObject {
  [propName: string]: any;
}

export interface RightTemplate{
  type?: CardRightType;
  /**
   * 扩展信息表单
   */
  extInfoForm?: AutoFormParams<IObject>;
  /**
   * 权益名称
   */
  name?: string;
  /**
   * 简介
   */
  description?: string;
  /**
   * 分组ID
   */
  group_type?: number;
}

export enum EnumRightGroup {
  '基础权益' = 1,
}

export interface ICardRightTemplateMap {
  [propName: number]: RightTemplate;
}

export const cardRightTemplateMap: ICardRightTemplateMap = {
  1: {
    type: 1,
    name: '包邮',
    group_type: 1,
  },
  2: { type: 2, name: '消费折扣', group_type: 1, extInfoForm: {
    fields: [
      { field: 'discount', label: '折扣额度', value: '', type: 'number', widget: 'text', widgetProps: { type: 'number', placeholder: '填写数字（例如: 10 -> 一折）' } }
    ],
    rule: {
      discount: [{      
        required: true,
        message: '请填写折扣额度'
      }, {
        enable: values => values.discount !== '',
        validator: (rule, value, error) => error(value <= 0 ? '折扣额度不可小于等于0' : value >= 100 ? '折扣额度不能大于100' : undefined)
      }]
    }
  } },
};
